import 'package:duuchin/config/app_colors.dart';
import 'package:flutter/material.dart';
import 'view/root_pages/home_page.dart';
import 'view/root_pages/music_page.dart';
import 'view/root_pages/create_media.dart';
import 'view/root_pages/tiny_video.dart';
import 'view/root_pages/profile_page.dart';


class RootPage extends StatefulWidget {
  const RootPage({Key key}) : super(key: key);

  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  int _currindex = 0; // 初始化选中首页
  final List<BottomNavigationBarItem> _bottomNavList = []; // 底部集合
  List _pageList = [HomePage(), MusicPage(), CreateMedia(),TinyVideo(),ProfilePage()]; // 引入首页的五个配置文件信息

  /// 参数类型定义
  Map<String, String> _bittomNames = {
    'home': '首页',
    'music': '音乐',
    'create_media': '',
    'tiny_video': '小视频',
    'profile': '我的'
  };

  // 定义widget 添加
  BottomNavigationBarItem _bottomNavBarItem(String name, String title) {
    double iconSize = 24;
    return BottomNavigationBarItem(
      icon: Image.asset('assets/images/icons/${name}.png',
          width: iconSize, height: iconSize),
      activeIcon: Image.asset('assets/images/icons/${name}_active.png',
          width: iconSize, height: iconSize),
      label: title,
      tooltip: ''
    );
  }

  // 底部按钮
  void _onTabClick(int index) {
    if (index == 2) {
      return _oncreate();
    }
    this.setState(() {
      _currindex = index;
    });
  }

  void _oncreate() {
    this.setState(() {
      _currindex = 2;
    });
  }
  /// 中间凸起按钮
  Widget _createButton() {
    return Container(
      width: 44,
      height: 44,
      margin: EdgeInsets.only(top: 56),
      child: FloatingActionButton(
        child: Image.asset('assets/images/icons/create_media.png'),
        backgroundColor: AppColors.nav,
        onPressed: _oncreate,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _bittomNames.forEach((key, value) {
      _bottomNavList.add(_bottomNavBarItem(key, value));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pageList[_currindex],
      bottomNavigationBar: BottomNavigationBar(
        items: _bottomNavList,
        currentIndex: _currindex,
        fixedColor: Colors.black.withOpacity(0.8),
        onTap: _onTabClick,
        type: BottomNavigationBarType.fixed,
      ),
      floatingActionButton: _createButton(),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}
